<template>
  <div class="home">
    <van-nav-bar
      :title="deviceInfo.scribe"
      left-arrow
      @click-left="onClickLeft"
    >
    </van-nav-bar>
    <div class="home-content">
      <p class="home-content-area-text">基本信息</p>
      <div class="home-content-info">
        <li
          class="home-content-info-item"
          @click="onDevices(item, index)"
          v-for="(item, index) in infoData"
          :key="item"
        >
          <div class="home-content-info-item-title">
            <p class="home-content-info-item-title-name">{{ item.name }}</p>
          </div>
          <p class="home-content-info-item-data">
            {{ item.content }}
          </p>
        </li>
      </div>
      <p class="home-content-area-text" v-if="controlData.length !== 0">
        设备控制
      </p>
      <div class="home-content-echarts" v-for="item in controlData" :key="item">
        <van-cell center size="large" :label="item.content" :title="item.name">
          <template #right-icon>
            <van-switch
              v-model="item.switch"
              @change="onControlChange(item)"
              size="25"
            />
          </template>
        </van-cell>
      </div>
    </div>
  </div>
</template>

<script>
import { Toast } from "vant";
import * as echarts from "echarts";
var send = {};
export default {
  name: "interface_page_index",
  components: {},
  created: function () {},
  mounted: function () {
    this.initInterface();
    console.log(this.paramsRes);
  },
  data: function () {
    return {
      mqttRes: {},
      paramsRes: {},
      deviceInfo: {},
      charts: [],
      tableData: [],
      iData: [],
      cresData: [],
      camera: "",
      infoData: [
        {
          name: "电机状态",
          content: "--",
        },
      ],
      controlData: [
        {
          id: 0,
          name: "开关",
          content: "--",
          switch: false,
        },
        {
          id: 1,
          name: "方向",
          content: "反转",
          switch: false,
        },
      ],
    };
  },
  watch: {
    mqttRes: {
      handler(newVal) {
        switch (newVal.State) {
          case 0:
            this.infoData[0].content = "锁死";
            this.controlData[0].content = "关闭";
            this.controlData[0].switch = false;
            break;
          case 1:
            this.infoData[0].content = "反转";
            this.controlData[1].switch = false;
            this.controlData[0].content = "开启";
            this.controlData[0].switch = true;
            this.controlData[1].content = "反转";
            break;
          case 2:
            this.infoData[0].content = "正转";
            this.controlData[1].switch = true;
            this.controlData[0].content = "开启";
            this.controlData[0].switch = true;
            this.controlData[1].content = "正转";
            break;
          default:
            break;
        }
      },
      deep: true,
    },
  },
  activated: function () {
    // 在 keepAlive 被激活的时候，调用指定加载页面组件的方法
    console.log("indexkeepalive");
    console.log(this.paramsRes);
  },
  methods: {
    initInterface() {
      this.paramsRes = this.$route.params;
      this.deviceInfo = this.paramsRes.deviceInfo;
      this.mqttRes = this.paramsRes.mqttMsg;
    },
    onClickLeft() {
      this.$router.back(-1);
    },
    onControlChange(item) {
      navigator.vibrate(100);
      var val;
      console.log(item);
      if (item.id === 0) {
        if (item.switch) {
          if (this.controlData[1].content === "正转") {
            val = 2;
          } else if (this.controlData[1].content === "反转") {
            val = 1;
          }
        } else {
          val = 0;
        }
      } else if (item.id === 1) {
        if (this.controlData[0].switch) {
          if (item.switch) {
            val = 2;
          } else {
            val = 1;
          }
        }
        else{
          if (item.switch) {
            this.controlData[1].content = "正转"
          } else {
           this.controlData[1].content = "反转"
          }
          return;
        }
      }
      var send = {};

      // if (this.controlData[0].content === "开启") {
      // } else if (this.controlData[0].content === "关闭") {
      //   val = 0;
      // }
      send["State"] = val;
      this.$mqtt.publish(this.deviceInfo.mqttSub, send);
    },
  },
};
</script>

<style lang="scss" scoped>
.home {
  width: 100%;
  height: 100%;
  overflow: auto;
  display: flex;
  flex-flow: column;
  position: absolute;
  &-content {
    height: 100%;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    align-items: left;
    justify-content: left;
    transition: 0.3s;
    overflow-x: hidden;
    &-cam {
      -webkit-animation: xhe 0.4s ease;
      //position: relative;
      margin: 0 20px 0 20px;
      padding: 10px 10px;
      border-radius: 20px 20px 20px 20px;
      box-shadow: 0 0 16px 0 rgba(0, 0, 0, 0.3);
      background-color: rgba(255, 255, 255, 0.85);
      backdrop-filter: blur(20px) saturate(180%);
      background-attachment: fixed;
      display: flex;
      justify-content: center;
      align-items: center;
      width: auto;
      height: 70vw;
      &-img {
        border-radius: 20px 20px 20px 20px;
        width: 100%;
        height: auto;
        min-height: 70vw;
      }
    }
    &-myecharts {
      -webkit-animation: xhe 0.4s ease;
      text-align: center;
      margin: 0 16px 15px;
      border-radius: 20px 20px 20px 20px;
      // box-shadow: 0 0 16px 0 rgba(0, 0, 0, 0.3);
      width: auto;
      //height: 40vh;
      display: flex;
      position: relative;
      flex-direction: column;
      justify-content: left;
      background-color: rgba(255, 255, 255, 0.85);
      backdrop-filter: blur(20px) saturate(180%);
      background-attachment: fixed;
    }
    &-echarts {
      -webkit-animation: xhe 0.4s ease;
      margin: 0 16px 0px;
      width: auto;
      display: flex;
      position: relative;
      flex-direction: column;
      justify-content: left;
      background-attachment: fixed;
    }
    &-control {
      border-radius: 20px;
      margin-top: 0px;
      margin-left: 20px;
      margin-right: 20px;
      margin-bottom: 20px;
    }
    &-area {
      &-text {
        -webkit-animation: hue 0.6s ease;
        //transition: all 0.6s ease;
        //transform: translateY(100px);
        //transition: .5s;
        margin-left: 20px;
        font-size: 5vw;
        color: white;
        font-weight: 900;
      }
    }
    &-text {
      margin: px2rem(20);
      font-size: px2rem(30);
      color: white;
      font-weight: 600;
      margin-bottom: px2rem(30);
    }
    &-shebei {
      margin-left: px2rem(20);
      margin-bottom: px2rem(5);
      font-size: px2rem(14);
      color: white;
      font-weight: 400;
    }
    &-info {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      padding: 0 16px 0 16px;

      &-item {
        -webkit-animation: xhe 0.4s ease;
        height: auto;
        // transition: 0.4s;
        margin: 5px;
        text-align: center;
        padding: 5px;
        border-radius: 20px 20px 20px 20px;
        // box-shadow: 0 0 16px 0 rgba(0, 0, 0, 0.3);
        width: calc(50% - 20px);
        // height: calc(50% - 10px);
        display: flex;
        position: relative;
        flex-direction: column;
        justify-content: left;
        background-color: rgba(255, 255, 255, 0.85);
        backdrop-filter: blur(20px) saturate(180%);
        background-attachment: fixed;
        &-title {
          display: flex;
          flex-direction: row;
          align-items: center;

          &-name {
            margin: auto;
            transition: 0.4s;
            text-align: left;
            color: black;
            //margin-top: 0;
            font-size: 4vw;
          }
        }

        &-data {
          transition: 0.4s;
          // text-align: right;
          color: black;
          margin: auto;
          font-size: 9vw;
          font-weight: 500;
        }
      }
    }
  }
}
.custom-button {
  width: auto;
  padding: 5px 10px;
  color: #fff;
  font-size: 4vw;
  line-height: 5vw;
  text-align: center;
  background-color: #0aadee;
  border-radius: 20px;
}
.van-cell {
  margin: 0 0 10px 0;
  width: auto;
  border-radius: 10px;
  background-color: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(20px) saturate(180%);
}
@-webkit-keyframes hue {
  from {
    transform: translateX(100px);
  }
  to {
    transform: translateY(0);
  }
}
@-webkit-keyframes xhe {
  from {
    transform: scale(0);
    // transform: translateX(50vw);
  }
  to {
    transform: scale(1);
    // transform: translateY(0);
  }
}
</style>
